package com.tqjc.area.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.tqjc.area.entity.RollerStock;
import com.tqjc.area.mapper.RollerStockMapper;
import com.tqjc.area.service.RollerStockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @author: pzy
 * @date: 2023年05月17日 17:48
 */

@Service
@DS("baotou")
public class RollerStockServiceImpl implements RollerStockService {
    @Resource
    private RollerStockMapper rollerStockMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RollerStock queryById(Integer id){
        return rollerStockMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param rollerStock 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<RollerStock> paginQuery(RollerStock rollerStock, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<RollerStock> queryWrapper = new LambdaQueryWrapper<>();
        //2. 执行分页查询
        Page<RollerStock> pagin = new Page<>(current , size , true);
        IPage<RollerStock> selectResult = rollerStockMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param rollerStock 实例对象
     * @return 实例对象
     */
    @Override
    public RollerStock insert(RollerStock rollerStock){
        rollerStockMapper.insert(rollerStock);
        return rollerStock;
    }

    /**
     * 更新数据
     *
     * @param rollerStock 实例对象
     * @return 实例对象
     */
    @Override
    public RollerStock update(RollerStock rollerStock){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<RollerStock> chainWrapper = new LambdaUpdateChainWrapper<RollerStock>(rollerStockMapper);
        //2. 设置主键，并更新
        chainWrapper.set(RollerStock::getId, rollerStock.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(rollerStock.getId());
        }else{
            return rollerStock;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = rollerStockMapper.deleteById(id);
        return total > 0;
    }
}
